﻿//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Licensed under the MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////
#include "CmdRemoveSelectedPaperOrViewPort.h"
#include "GbmpUiPlatformCommandRegister.h"
#include "GbmpUiPlatformCommandIds.h"

#include "IUiDocument.h"

#include "IUiDocumentViewManager.h"
#include "UiCommonDialog.h"
#include "CommandParameters.h"
#include "IUserTransaction.h"
#include "IDrawingLayoutManager.h"
#include "IDrawingLayout.h"
#include "IDrawingViewport.h"
#include "IGraphicsNodeReference.h"
#include "ISelection.h"
#include "EnableCompileWarning_The_LAST_IncludeInCpp.h"

using namespace gcmp;
CmdRemoveSelectedPaperOrViewport::CmdRemoveSelectedPaperOrViewport()
    : CommandBase(ID_CMD_REMOVE_SELECTED_PAPER_OR_VIEWPORT, true)
{
}

CmdRemoveSelectedPaperOrViewport::~CmdRemoveSelectedPaperOrViewport()
{
}

gcmp::OwnerPtr<gcmp::IAction> CmdRemoveSelectedPaperOrViewport::ExecuteCommand(const gcmp::CommandParameters& cmdParams)
{
    std::wstring itemType;
    auto modelViewTypeIter = cmdParams.find(L"itemType");
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(modelViewTypeIter != cmdParams.end(), L"无法找到参数itemType",L"GDMPLab",L"2024-03-30");
    itemType = modelViewTypeIter->second.AsWString();

    std::wstring itemName;
    auto modelViewNameIter = cmdParams.find(L"itemName");
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(modelViewNameIter != cmdParams.end(), L"无法找到参数itemName",L"GDMPLab",L"2024-03-30");
    itemName = modelViewNameIter->second.AsWString();
    IUiDocument* pUIDoc = IUiDocumentViewManager::Get()->GetCurrentUiDocument();
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pUIDoc, L"pUIDoc为空",L"GDMPLab",L"2024-03-30");
    IDocument *pDoc = pUIDoc->GetDbDocument();
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pDoc, L"pDoc为空",L"GDMPLab",L"2024-03-30");
    std::wstring errorMessage = L"";
    IDrawingLayoutManager* pLayoutManager = IDrawingLayoutManager::Get(pDoc, &errorMessage);
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pLayoutManager, L"pLayoutManager为空",L"GDMPLab",L"2024-03-30");

    bool IsDeleted = false;
    if (itemType == L"Drawing Layout")
    {
        //删除图纸
        std::wstring currentLayoutName;
        auto modelViewNameIter = cmdParams.find(L"currentLayoutName");
        DBG_WARN_AND_RETURN_NULLPTR_UNLESS(modelViewNameIter != cmdParams.end(), L"无法找到参数itemName",L"GDMPLab",L"2024-03-30");
        currentLayoutName = modelViewNameIter->second.AsWString();

        if (currentLayoutName == itemName)
        {
            UiCommonDialog::ShowMessageBox(GBMP_TR(L"删除图纸"), GBMP_TR(L"不能删除当前图纸。"), (int)(UiCommonDialog::ButtonType::OK));
            return nullptr;
        }
        else
        {
            if (UiCommonDialog::ButtonType::OK == UiCommonDialog::ShowMessageBox(GBMP_TR(L"删除图纸"), GBMP_TR(L"确定删除该图纸？"), (int)(UiCommonDialog::ButtonType::OK)))
            {
                OwnerPtr<IUserTransaction> opTransaction = IUserTransaction::Create(pDoc, GBMP_TR(L"删除图纸"), true);
                IsDeleted = pLayoutManager->DeleteLayout(itemName);
                opTransaction->Commit();
            }
            else
                return nullptr;
        }
    }
    else if (itemType == L"Drawing Viewport")
    {
        //删除视口
            if (UiCommonDialog::ButtonType::OK == UiCommonDialog::ShowMessageBox(GBMP_TR(L"删除视口"), GBMP_TR(L"确定删除该视口？"), (int)(UiCommonDialog::ButtonType::OK)))
            {
                std::wstring ViewportCorrespondedToLayout;
                auto modelViewNameIter = cmdParams.find(L"ViewportCorrespondedToLayout");
                DBG_WARN_AND_RETURN_NULLPTR_UNLESS(modelViewNameIter != cmdParams.end(), L"无法找到参数ViewportCorrespondedToLayout",L"GDMPLab",L"2024-03-30");
                ViewportCorrespondedToLayout = modelViewNameIter->second.AsWString();

                IDrawingLayout* pViewPortCorrespondedToLayout = pLayoutManager->GetLayout(ViewportCorrespondedToLayout);
                DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pViewPortCorrespondedToLayout, L"pViewPortCorrespondedToLayout为空",L"GDMPLab",L"2024-03-30");
                OwnerPtr<IUserTransaction> opTransaction = IUserTransaction::Create(pDoc, GBMP_TR(L"删除视口"), true);

                // 清空选择集
                {
                    IDrawingViewport* pViewportToDel = pViewPortCorrespondedToLayout->GetViewport(itemName);
                    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pViewportToDel, L"viewport is null",L"GDMPLab",L"2024-03-30");
                    
                    // 被激活的视口不能删除
                    if (pViewportToDel->IsEnableActive() && pViewportToDel->IsActive())
                    {
                        UiCommonDialog::ShowMessageBox(GBMP_TR(L"删除视口"), GBMP_TR(L"无法删除，因为该视口正处于激活状态"), (int)(UiCommonDialog::ButtonType::OK));
                        return nullptr;
                    }
                    
                    // 保存viewport id
                    IElement* pElement = pViewportToDel->GetOwnerElement();
                    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pElement, L"pElement为空",L"GDMPLab",L"2024-03-30");
                    ElementId vpToDelId = pElement->GetElementId();
                    OwnerPtr<IGraphicsNodeReference> gnode = IGraphicsNodeReference::Create();
                    gnode->SetElementId(vpToDelId);
                    ISelection::Get()->DeleteGraphicsNodeReference(pDoc, *gnode);
                }

                // 删除视口
                IsDeleted = pViewPortCorrespondedToLayout->DeleteViewport(itemName);

                opTransaction->Commit();
            }
            else
                return nullptr;

        }

    if (IsDeleted)
    {
        UiCommonDialog::ShowMessageBox(GBMP_TR(L"删除"), GBMP_TR(L"删除成功"), (int)(UiCommonDialog::ButtonType::OK));
    }
    else
    {
        UiCommonDialog::ShowMessageBox(GBMP_TR(L"删除"), GBMP_TR(L"删除失败"), (int)(UiCommonDialog::ButtonType::OK));
    }

    return nullptr;
}

REGISTER_GM_COMMAND(CmdRemoveSelectedPaperOrViewport)
